package com.zxc.pricing.decorator;

import com.zxc.enums.Fruit;
import com.zxc.pricing.PriceCalculator;

import java.util.Map;
/**
 * 草莓打折装饰器
 */
public class StrawberryDiscountDecorator implements PriceCalculator {
    private final PriceCalculator calculator;
    private final double discount; // 0.8 表示8折

    public StrawberryDiscountDecorator(PriceCalculator calculator, double discount) {
        this.calculator = calculator;
        this.discount = discount;
    }

    @Override
    public double calculateTotal(Map<Fruit, Integer> fruits) {
        // 先计算基础价格
        double total = calculator.calculateTotal(fruits);

        // 计算草莓原价部分
        Integer strawberryWeight = fruits.get(Fruit.STRAWBERRY);
        if (strawberryWeight != null && strawberryWeight > 0) {
            double strawberryOriginalPrice = strawberryWeight * Fruit.STRAWBERRY.getPrice();
            double strawberryDiscountedPrice = strawberryOriginalPrice * discount;
            // 调整总价格：减去原价，加上折扣价
            total = total - strawberryOriginalPrice + strawberryDiscountedPrice;
        }

        return total;
    }
}
